﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using Users;
using Lists;
using Sim;

namespace Sim
{
    [Serializable()]
    public class Dive
    {
        public string diveCode { get; set; }
        public double diveDifficulty { get; set; }
        public int diveHeight { get; set; }
        public double calculatedScore { get; set; }
        public List<Score> diveScore { get; set; }
        private Random random = new Random();

        public Dive()
        {
            diveCode = "";
            diveDifficulty = 0.0;
            diveHeight = 0;
            calculatedScore = 0;
            diveScore = new List<Score>();
        }

        public Dive(string diveCode)
        {
            this.diveCode = diveCode;
            diveDifficulty = 0.0;
            diveHeight = 0;
            calculatedScore = 0;
            diveScore = new List<Score>();
        }


        public void setJudgeScore(double scoreOfJump, int idOfJudge)
        {
            diveScore.Add(new Score(scoreOfJump, idOfJudge));
        }

        public double CalcPts() //Räknar ut antal poäng som ges av Dommmare och tar bort största/minsta värde
        {
            double totalpts = 0;

            diveScore.Sort((x, y) => x.scoreOfJump.CompareTo(y.scoreOfJump));

            
            foreach (var points in diveScore)
                totalpts += points.scoreOfJump;

            if (diveScore.Count() == 5)
                totalpts -= (diveScore[0].scoreOfJump + diveScore[diveScore.Count()-1].scoreOfJump);
            else if (diveScore.Count() == 7)
                totalpts -= (diveScore[0].scoreOfJump + diveScore[1].scoreOfJump + diveScore[diveScore.Count() - 1].scoreOfJump + diveScore[diveScore.Count() - 2].scoreOfJump);
            return totalpts; //CalcPts() * DD = Diverspoints
        }

        public double CalcPtsnDD(string divecode, int height)
        {
            double DDval = 0;
            for (int i = 0; i < DDList.ddList.Count(); i++)
            {
                if (divecode == DDList.ddList[i].diveCode && height == DDList.ddList[i].diveHeight)
                {
                    DDval = DDList.ddList[i].diveDifficulty;
                    break;
                }
            }
            calculatedScore = DDval * CalcPts();

            diveCode = divecode;
            diveHeight = height;
            diveDifficulty = DDval;
            return calculatedScore;
        }

        //public double CalcFailure(double DDinput)
        //{

        //    double DDrate = 0;
        //    DDrate = DDinput * 5;
        //    int failiureRate = 0;
        //    int failMargin = 0;
        //    int successMargin = 0;
        //    int failprocent = Convert.ToInt32(DDrate);

        //    failiureRate = random.Next(0, 101);
        //    failMargin = random.Next(0, 50);
        //    successMargin = random.Next(50, 101);
        //    DDrate = DDrate / 5;

        //    if (DDrate >= 1.00 && DDrate <= 4.80)
        //    {
        //        if (failiureRate >= 0 && failiureRate <= failprocent)
        //        {
        //            //Console.WriteLine("The jump failed\n");
        //            //Console.WriteLine("Failiure rate: " + failiureRate + "\n");
        //            //Console.WriteLine("Fail margin: " + failMargin + "\n");
        //            //Console.WriteLine("Failprocent: " + failprocent + "\n");
        //            return DDrate;
        //        }
        //        else
        //        {
        //            //Console.WriteLine("The jump was a success\n");
        //            //Console.WriteLine("Success rate: " + failiureRate + "\n");
        //            //Console.WriteLine("Success margin: " + successMargin + "\n");
        //            //Console.WriteLine("Failprocent: " + failprocent + "\n");
        //            return DDrate;
        //        }
        //    }
        //    return DDrate;
        //}

    }
}
